(function (window) {
    var _dragDataLit = [];
    var _current = null;
    var _callback = null;
    var drag = {
        init: function (className, dragDataLit, callback) {
            addDragItem(document.getElementsByClassName(className));
            _dragDataLit = dragDataLit;
            _callback = callback;
        },
    };
    var tools = {
        on: function (el, type, listener) {
            el.addEventListener(type, listener);
        },
        addClass: function (el, className) {
            el.classList.add(className);
        },
        removeClass: function (el, cls) {
            if (arguments.length < 2) {
                el.classList.remove(cls);
            }
            else {
                for (var i = 1, len = arguments.length; i < len; i++) {
                    el.classList.remove(arguments[i]);
                }
            }
        }
    };
    function addDragItem(nodeList) {
        var _len = nodeList.length;
        for (var i = 0; i < _len; i++) {
            var el = nodeList[i];
            el.setAttribute('draggable', 'true');
            el.setAttribute('data-dragKey', i.toString());
            tools.on(el, 'dragstart', handleDragStart);
            tools.on(el, 'dragend', handleDragEnd);
            tools.on(el, 'drop', handleParentDrop);
        }
    }
    function handleDragStart(e) {
        var el = e.target;
        tools.addClass(el, 'dragging');
        _current = _dragDataLit[el.getAttribute('data-dragKey')];
        console.log(_current);
        e.dataTransfer.setData('text', JSON.stringify(_current));
    }
    function handleDragEnd(e) {
        _callback(_current);
        e.preventDefault();
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        return false;
    }
    function handleParentDrop(e) {
        e.preventDefault();
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        return false;
    }
    window.drag = drag;
})(window);
